<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/27
 * Time: 6:53
 */

namespace app\lib\Validate;


use app\lib\Exception\PayException;

class PayValidate extends BaseValidate
{
    protected $rule = [
        'products' => 'require|checkEmptys'
    ];
    
    protected $message = [
        'products' => '我看不到你的商品'
    ];

    protected $strRule = [
        'product_id' => 'require|isIntegerNumber',
        'count' => 'require|isIntegerNumber'
    ];

    protected function checkEmptys($values)
    {
        if(empty($values)) {
            throw new PayException();
        }

        foreach($values as $value) {
            $this->checkEmpty($value);
        }

        return true;
    }

    protected function checkEmpty($value)
    {
        $validate = new BaseValidate($this->strRule);
        if(!$validate->check($value)) {
            throw new PayException([
                'message' => '商品列表有误'
            ]);
        }
        return true;
    }
}